package kr.lgh.sms_creditcard.Category;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import kr.lgh.sms_creditcard.R;
import kr.lgh.sms_creditcard.BankType.BankInfo;
import kr.lgh.sms_creditcard.BankType.CustomAdapter;
import kr.lgh.sms_creditcard.DB.SQLiteDBHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CategoryToList extends Activity {
	
	Button btnCategory, btnDate, btnSearach, btnAverage;
	ArrayList<String> arType;
	ArrayList<BankInfo> arCategory;
	ArrayAdapter<String> typeAdapter;
	ListView list;
	TextView tv_categoryTotal;
	SQLiteDBHelper dbHelper;
	CustomAdapter adapter;
	SQLiteDatabase db;
	Cursor cursor;
	int mYear, mMonth, mDay;
	int mSelect = 0;
	int average = 0;
	String choice;
	boolean flag = false;

	final static String sql = "select _name from Category_Table";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_list);
		
		list = (ListView)findViewById(R.id.categorylist);
		btnCategory = (Button)findViewById(R.id.c_category);
		btnDate = (Button)findViewById(R.id.c_date);
		btnAverage = (Button) findViewById(R.id.btn_average_category);
		btnSearach = (Button)findViewById(R.id.s_search);
		tv_categoryTotal = (TextView)findViewById(R.id.tv_categoryTotal);
		arType = new ArrayList<String>();
		dbHelper = new SQLiteDBHelper(this);
		db = dbHelper.getReadableDatabase();
		
		cursor = db.rawQuery(sql, null);
		cursor.moveToFirst();
		
		while(!cursor.isAfterLast()){
			arType.add(cursor.getString(0));
			cursor.moveToNext();
		}
		
		typeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arType);
		
		Calendar cal = new GregorianCalendar();
		mYear = cal.get(Calendar.YEAR);
		mMonth = cal.get(Calendar.MONTH);
		mDay = cal.get(Calendar.DAY_OF_MONTH);
		btnDate.setText(mYear + ". " + (mMonth + 1));
		
	}
	
	public void mOnClick(View v){
		
		switch(v.getId()){
		
		case R.id.c_category:
			
			new AlertDialog.Builder(this)
			.setTitle("카테고리를 선택하세요.")
			.setSingleChoiceItems(
					(String[]) arType
							.toArray(new String[arType.size()]),
					mSelect, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							mSelect = which;
						}
					})
			.setPositiveButton("확인",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog,
								int whichButton) {
							choice = arType.get(mSelect).toString();
							btnCategory.setText(choice);
						}
					}).setNegativeButton("취소", null).show();
			
			break;
			
		case R.id.c_date:
			new DatePickerDialog(CategoryToList.this, mDateSetListener, mYear,
					mMonth, mDay).show();
			break;
			
		case R.id.c_search:
			average = 0;
			final DecimalFormat df = new DecimalFormat("#,##0");
			Log.d("TAG", "choice : " + choice + " mYear : " + mYear + " mMonth : " + mMonth);
			if (choice != null && mYear != 0 && mMonth != 0) {
				arCategory = new ArrayList<BankInfo>();
				arCategory = dbHelper.categoryToList(choice, mYear, mMonth + 1);
				
				if(arCategory.size() == 0){
					Toast.makeText(this, "해당항목 없음", Toast.LENGTH_SHORT).show();
				}
				adapter = new CustomAdapter(this, arCategory);
				list.setAdapter(adapter);
				
				Log.d("TAG", "arCategory 갯수 : " + arCategory.size());
				for (BankInfo bank : arCategory) {
					String log = " / date : " + bank.getS_date()
							+ " / time : " + bank.getS_time() + " / money : "
							+ bank.getS_money() + " / place : "
							+ bank.getS_place() + " / card : "
							+ bank.getS_card();
					Log.d("TAG", log);
					average += bank.getS_money();
					Log.d("TAG", "average : " + average);
				}
				
				Log.d("TAG", "average : " + average + " / arCategory.size() : "
						+ arCategory.size());
				btnAverage.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {						
						if (!flag) {							
							btnAverage.setText("￦ "
									+ df.format((average) / arCategory.size())
									+ "원 / 일");
							tv_categoryTotal.setText("하루 평균 : ");
							flag = true;
						} else {
							btnAverage.setText("￦ " + df.format((average)) + "원");
							tv_categoryTotal.setText("월 합계 : ");
							flag = false;
						}
					}
				});

				if (!flag) {
					btnAverage.setText("￦ " + df.format(average)+ "원");
				}
				
			} else
				Toast.makeText(this, "값을 선택하시오", Toast.LENGTH_SHORT).show();
			
			break;
		}
	}
	
	DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;

			String date = mYear + ". " + (mMonth + 1) + ". " + mDay;
			btnDate.setText(date);
		}
	};
}
